package com.project.controller;

import com.project.bean.ClassBean;
import com.project.service.IClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("class")
public class ClassController {
    @Autowired
    private IClassService classService;

    /**
     * 查询所有班级对象
     *
     * @return 班级对象集合
     */
    @RequestMapping("findAll")
    public List<ClassBean> findAll() {
        return classService.findAll();
    }

    /**
     * 添加班级
     *
     * @param name    班级名称
     * @param teacher 带班老师
     * @param day     开班时间
     * @return "OK"
     */
    @RequestMapping("add")
    private String addClass(String name, String teacher, LocalDate day) {
        ClassBean classBean = new ClassBean(name, day, teacher);
        classService.add(classBean);
        return "OK";
    }

    /**
     * 根据班级名称获取该班级的id
     *
     * @param name 班级名称
     * @return 班级id
     */
    @RequestMapping("byName")
    public Integer byName(String name) {
        return classService.findByName(name).getId();
    }

    /**
     * 根据班级id，判断是否还存在学生，删除班级
     *
     * @param id 班级id
     * @return 成功 OK；失败 NO
     */
    @RequestMapping("del")
    public String del(int id) {
        if (classService.del(id)) {
            return "OK";
        }
        return "NO";
    }
}
